@extends('develop.layoutEdit')

@section('title')

    <li class="cur">
        <span>拆单设置</span>
    </li>

@endsection


@section('css')
    <style>
        .layer-go-back{
            display:none;
        }
    </style>
@endsection

@section('content')

    <div class="form-group">
        <label class="col-sm-3 control-label">开启拆单：</label>
        <div class="col-sm-9">
            <div class="layui-form">
                <input type="radio" name="is_split"  value="1"  title="是" @if( isset($configure['is_split']) && $configure['is_split'] == 1 ) checked @endif>
                <input type="radio" name="is_split"  value="0" title="否" @if( isset($configure['is_split']) && $configure['is_split'] == 0 ) checked @endif>
            </div>
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label">供应商维度拆单：</label>
        <div class="col-sm-9">
            <div class="layui-form">
                <input type="radio" name="supplier"  value="1"  title="开启" @if( isset($configure['supplier']) && $configure['supplier'] == 1 ) checked @endif>
                <input type="radio" name="supplier"  value="0" title="关闭" @if( isset($configure['supplier']) && $configure['supplier'] == 0 ) checked @endif>
            </div>
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label">仓库维度拆单：</label>
        <div class="col-sm-9">
            <div class="layui-form">
                <input type="radio" name="store"  value="1" title="开启" @if( isset($configure['store']) && $configure['store'] == 1 ) checked @endif>
                <input type="radio" name="store"  value="0" title="关闭" @if( isset($configure['store']) && $configure['store'] == 0 ) checked @endif>
            </div>
        </div>
    </div>

    <div class="form-group" >
        <label class="col-sm-3 control-label">商品属性维度拆单：</label>
        <div class="col-sm-9">
            <div class="layui-form">
                <input type="radio" name="goods_attr"  value="1" class="is_open" lay-filter="check" title="开启"  @if( isset($configure['goods_attr']) && $configure['goods_attr'] == 1 ) checked @endif>
                <input type="radio" name="goods_attr"  value="0" class="is_open" lay-filter="no_check" title="关闭" @if( isset($configure['goods_attr']) && $configure['goods_attr'] == 0 ) checked @endif>
            </div>
        </div>

        <div class="col-sm-5 col-sm-offset-3" style="margin-top: 10px;display: none;" id="goods">
            <label class="col-sm-2 control-label"></label>
            <div class="panel panel-default">
                <div class="panel-body">
                    @if( isset($configure['goods_attr_data']) && !empty($configure['goods_attr_data']) )
                        @foreach( $configure['goods_attr_data'] as $val )
                            <a class="btn btn-danger top-right-btn" style="margin-bottom:5px;" id="{{ $val['attribute_id'] }}" href="javascript: void(0);" role="button" onclick="plugin.remove(this,{{ $val['attribute_id'] }});">
                                {{ $val['attribute_name'] }}&nbsp;&nbsp<span class="glyphicon glyphicon glyphicon-remove" aria-hidden="true"></span>
                            </a>
                        @endforeach
                    @endif
                    <a class="btn btn-default top-right-btn" style="margin-top:-3px;" href="javascript: void(0);" role="button" onclick="plugin.tager();" id="begin">
                        <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 添加属性
                    </a>
                </div>
            </div>
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label"></label>
        <div class="col-sm-9">
            <input type="hidden" name="function_id" id="function_id" value="{{ $function_id or ''}}">
            <input type="button"  value="保存" onclick="func.edit();" class="btn btn-success">
        </div>
    </div>

@endsection

@section('js')
    <script>

        layui.use(['form'], function(){
            var form = layui.form();

            form.on('radio(check)', function(data){
                if ( this.checked ) {
                    $("#goods").show();
                }
            });
            form.on('radio(no_check)', function(data){
                if ( this.checked ) {
                    $("#goods").hide();
                }
            });
        });
        $(window).ready(function(){
            @if( $configure['goods_attr'] == 1 )
                $("#goods").show();
            @endif
        });

        var configure_json = 0 ;
        if ( !E.isEmpty({!! $configure_json or 0 !!}) ) {
            configure_json = eval( '(' + '{!! $configure_json or 0 !!}'  + ')' );
        }


        var func = {

            //接受属性弹层数据
            @if( !empty( $configure['goods_attr_data'] ) )
            attr_data:  configure_json['configure']['goods_attr_data'],
            @else
            attr_data:  { },
            @endif


            //保存
            edit:function(){

                var message_log = '' ;
                var configure = E.getFormValues('edit-form') ;

                if(configure.goods_attr == 1){
                    configure.goods_attr_data = func.attr_data;
                }

                //参数判断
                if( E.isEmpty( configure.function_id ) || !E.isInt( configure.function_id ) ) {
                    message_log += '参数错误<br/>' ;
                }
                if( message_log ) {
                    layer.alert( message_log , { icon:2 });
                    return false ;
                }

                layer.confirm("您确认保存该设置吗？",{icon:3,offset:'50px'}, function ( index ){
                    layer.close( index );
                    E.ajax( {
                        type:'get',
                        url: '/lar/develop/function/store/'+function_id,
                        data: configure,
                        dataType:'json',
                        // 返回值判断
                        success: function ( obj ){

                            if ( obj.code == 200 ) {
                                layer.alert( obj.message , { icon: 1,time : 1500 } ) ;
                            } else {
                                layer.alert( obj.message , { icon: 2 } ) ;
                            }
                        }
                    } ) ;
                });
            }

        };

        var plugin = {

            tager: function () {
                layer.open({
                    title: '选择商品属性',
                    type: 2,
                    area: ['900px', '500px'],
                    content: '/postsystem/app/plugin/goodsAttrPlugin.pscript?operFlg=1&multiple=1'
                });


            },

            goodsAttr: function (data) {

                var html = '';
                $.each(data, function (k, v) {

                    //判断项目是否已经添加过
                    if( $("#"+v.attribute_id).length > 0 ) {
                        return true ;
                    }

                    html += '<a class="btn btn-danger top-right-btn"  style="margin-bottom:5px;"id="'+v.attribute_id+'" href="javascript: void(0);" role="button" onclick="plugin.remove(this,' + v.attribute_id + ');">';
                    html += v.attribute_name + '&nbsp;&nbsp<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>';
                    html += '</a>&nbsp;&nbsp;';

                    func.attr_data[v.attribute_id] = {
                        attribute_id: v.attribute_id,
                        attribute_name: v.attribute_name,
                        remark: v.remark,
                        sortOrder: v.sortOrder,
                        entering_type: v.entering_type,
                        useFlg: v.useFlg
                    };
                    console.log(func.attr_data[v.attribute_id]);
                });

                $('.panel-body').prepend(html);

            },

            remove: function (obj, k) {
                delete( func.attr_data[k] );
                $(obj).remove();
            }
        }

    </script>
@endsection